﻿package component.formula.operation;

import java.math.BigDecimal;

import component.formula.exception.FormulaException;
import component.formula.util.CalculateUtil;

public abstract class Operation {
	protected String leftStr;
	protected String rightStr;
	
	protected BigDecimal leftBigDecimal;
	protected BigDecimal rightBigDecimal;
	
	public abstract String getOperation();
	
	/**
	 * 计算
	 * @param left
	 * @param right
	 * @return
	 * @throws FormulaException
	 */
	public abstract Object calculate(String left,String right) throws FormulaException;
	
	protected void verify(String left,String right) throws FormulaException{
		FormulaException.debug("Operator:" + left + this.getOperation() + right);
		if(left==null || "".equals(left)){
			throw new FormulaException("缺少左操作数");
		}
		if(right==null || "".equals(right)){
			throw new FormulaException("缺少右操作数");
		}
	}
	
	protected void changeToBigDecimal(String left,String right) throws FormulaException{
		this.verify(left, right);
		if(!CalculateUtil.isNumberAndPoint(left)){
			throw new FormulaException("左操作数不是数字：" + left);
		}
		if(!CalculateUtil.isNumberAndPoint(right)){
			throw new FormulaException("右操作数不是数字：" + right);
		}
		BigDecimal leftBigDecimal = new BigDecimal(left);
		BigDecimal rightBigDecimal = new BigDecimal(right);
		this.leftBigDecimal = leftBigDecimal;
		this.rightBigDecimal = rightBigDecimal;
	}
}
